package com.party.common;

public class PageToolPagination {
    private String url;
    private Page page;
    private int currentPage = 1;
    private int pageSize = 10;
    private int totalPageCount;
    private int cursor = 5;
    private int lineCursor = 10;

    private String urlString;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public PageToolPagination() {
    }

    public String getPageString(String url, Page page) {
        if (url.indexOf("?") != -1) {
            this.url = "<a class='not_current_page' href='javascript:;' ><span onclick=" + url + " value=";
            this.urlString = url + " value=";
        } else {
            this.url = "<a class='not_current_page' href='javascript:;'> <span  onclick=" + url + " value=";
            this.urlString = url + " value=";
        }

        this.page = page;
        this.currentPage = page.getCurrentPage();
        this.pageSize = page.getPageSize();
        this.totalPageCount = page.getTotalPageCount();
        return getLeftTools() + getCenter() + getRightTools() + "";
    }

    private String getLeftTools() {
        StringBuffer leftString;
        if (currentPage == 1) {
            leftString = new StringBuffer("");
        } else {
            leftString = new StringBuffer(getUrl() + " id=1>« 首页</span>");
        }
        return leftString.toString();
    }

    private String getRightTools() {
        StringBuffer rightString;
        if (currentPage == totalPageCount || totalPageCount == 0) {
            rightString = new StringBuffer("");
        } else {
            rightString = new StringBuffer(getUrl() + (getTotalPageCount()) + " id="+(getTotalPageCount())+">尾页 »</span></a>");
        }
        return rightString.toString();
    }

    private String getCenter() {
        StringBuffer centerString = new StringBuffer("");
        if (totalPageCount <= lineCursor) {
            for (int i = 1; i < this.totalPageCount + 1; i++) {
                if (i == this.currentPage) {
                    centerString.append("<span onclick=\"pagination(this)\">" + i + "</span></a>");
                } else {
                    centerString.append(getUrl() + i + ">" + i + "</span></a>  ");
                }
            }
        } else {
            int s = currentPage - cursor;
            if (s < 0) {
                for (int i = 1; i < lineCursor + 1; i++) {
                    if (i == this.currentPage) {
                        centerString.append("<a href='javascript:;' class='on'><span onclick=\"pagination(this)\">" + i + "</span></a>");
                    } else {
                        centerString.append(getUrl() + i + ">" + i + "</span> </a> ");
                    }
                }
            } else {
                if (s + lineCursor > totalPageCount) {
                    for (int i = totalPageCount - lineCursor + 1; i <= totalPageCount; i++) {
                        if (i == this.currentPage) {
                            centerString.append("<a href='javascript:;' class='on'><span onclick=\"pagination(this)\">" + i + "</span></a>");
                        } else {
                            centerString.append(getUrl() + i + ">" + i + "</span> </a>");
                        }
                    }
                } else {
                    for (int i = s + 1; i < lineCursor + s + 1; i++) {
                        if (i == this.currentPage) {
                            centerString.append("<a href='javascript:;' class='on'> <span onclick=\"pagination(this)\">" + i + "</span></a>");
                        } else {
                            centerString.append(getUrl() + i + ">" + i + "</span></a> ");
                        }
                    }
                }
            }
        }
        return centerString.toString();
    }
}
